package src.controller;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

@SuppressWarnings("unused")
public class mima extends Application {
    private static final int WINDOW_WIDTH = 425;
    private static final int WINDOW_HEIGHT = 340;

    @FXML
    private PasswordField oldPasswordField;
    @FXML
    private PasswordField newPasswordField;
    @FXML
    private PasswordField confirmPasswordField;
    @FXML
    private ToggleButton oldPasswordToggle;
    @FXML
    private ToggleButton newPasswordToggle;
    @FXML
    private ToggleButton confirmPasswordToggle;
    @FXML
    private Button cancelButton;
    @FXML
    private Button confirmButton;

    public mima() {
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // 使用绝对路径确保能找到FXML文件
        FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/mima.fxml"));
        loader.setController(this);
        Parent root = loader.load();

        Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
        primaryStage.setTitle("重置密码");
        primaryStage.setScene(scene);
        primaryStage.setMinWidth(450);
        primaryStage.setMinHeight(350);
        primaryStage.show();
    }

    @FXML
    private void initialize() {
        setupPasswordToggle(oldPasswordField, oldPasswordToggle);
        setupPasswordToggle(newPasswordField, newPasswordToggle);
        setupPasswordToggle(confirmPasswordField, confirmPasswordToggle);

        // 设置按钮事件
        setupButtonActions();
    }

    private void setupPasswordToggle(PasswordField passwordField, ToggleButton toggleButton) {
        TextField visibleField = new TextField();
        visibleField.setPrefWidth(380);
        visibleField.setPrefHeight(40);
        visibleField.setStyle("-fx-background-radius: 4; -fx-border-radius: 4; -fx-border-color: #E0E0E0; -fx-padding: 10; -fx-font-size: 14;");
        visibleField.setVisible(false);
        visibleField.setManaged(false);

        if (passwordField.getParent() instanceof HBox) {
            HBox parent = (HBox) passwordField.getParent();
            parent.getChildren().add(visibleField);

            visibleField.textProperty().bindBidirectional(passwordField.textProperty());

            toggleButton.setOnAction(event -> {
                boolean isSelected = toggleButton.isSelected();
                if (isSelected) {
                    passwordField.setVisible(false);
                    passwordField.setManaged(false);
                    visibleField.setVisible(true);
                    visibleField.setManaged(true);
                    toggleButton.setText("🔒");
                } else {
                    visibleField.setVisible(false);
                    visibleField.setManaged(false);
                    passwordField.setVisible(true);
                    passwordField.setManaged(true);
                    toggleButton.setText("👁️");
                }
            });
        }
    }

    private void setupButtonActions() {
        // 取消按钮事件 - 关闭窗口
        cancelButton.setOnAction(event -> {
            // 获取当前窗口并关闭
            Stage stage = (Stage) cancelButton.getScene().getWindow();
            stage.close();
        });

        // 确认按钮事件 - 执行密码重置逻辑
        confirmButton.setOnAction(event -> {
            handlePasswordReset();
        });
    }

    private void handlePasswordReset() {
        String oldPassword = oldPasswordField.getText();
        String newPassword = newPasswordField.getText();
        String confirmPassword = confirmPasswordField.getText();

        // 验证输入
        if (oldPassword.isEmpty() || newPassword.isEmpty() || confirmPassword.isEmpty()) {
            showAlert(Alert.AlertType.ERROR, "错误", "请填写所有密码字段");
            return;
        }

        if (!newPassword.equals(confirmPassword)) {
            showAlert(Alert.AlertType.ERROR, "错误", "新密码和确认密码不一致");
            return;
        }

        if (newPassword.length() < 6) {
            showAlert(Alert.AlertType.ERROR, "错误", "新密码长度至少为6位");
            return;
        }

        // 这里添加实际的密码重置逻辑
        // 例如：验证原密码、更新数据库等

        // 模拟成功重置
        Alert successAlert = new Alert(Alert.AlertType.INFORMATION);
        successAlert.setTitle("成功");
        successAlert.setHeaderText(null);
        successAlert.setContentText("密码重置成功");

        // 在提示框关闭后执行退出程序操作
        successAlert.showAndWait().ifPresent(response -> {
            // 清空字段
            oldPasswordField.clear();
            newPasswordField.clear();
            confirmPasswordField.clear();

            // 退出整个应用程序
            Platform.exit();
            System.exit(0);
        });
    }

    private void showAlert(Alert.AlertType alertType, String title, String message) {
        Alert alert = new Alert(alertType);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }

    public static void main(String[] args) {
        launch(args);
    }
}